/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.view.content.ease {
	import com.googlecode.milib.animation.static_.MLEase;
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.data.ScrollableArray;
	import com.googlecode.milib.data.TabsText;
	import com.googlecode.milib.dbg.model.content.MLDBGEasesList;
	import com.googlecode.milib.dbg.view.content.AbstractMLDBGWindowContent;
	import com.googlecode.milib.dbg.view.content.ease.EaseTypeInfo;
	import com.googlecode.milib.display.service.MLTextField;
	import com.googlecode.milib.display.ui.checkbox.MCCheckBox;
	import com.googlecode.milib.display.ui.combobox.MCComboBox;
	import com.googlecode.milib.display.ui.combobox.MCComboBoxItemVO;
	import com.googlecode.milib.util.MLArrayUtil;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.MovieClip;
	import flash.system.System;
	import flash.utils.getDefinitionByName;

	/**
	 * @author Marek Brun
	 */
	public class MLDBGEaseEditView extends AbstractMLDBGWindowContent implements IMLBroadcasterListener {

		private static var easesInfo:Array;		private static var easesListElements:Array;		private var model:MLEase;
		private var easesListComboBox:MCComboBox;
		private var sliders:EasePropsSliders;
		private var easesScrolledArray:ScrollableArray;
		private var modelEaseType:EaseTypeInfo;
		private var chart:EaseChart;
		private var checkDouble:MCCheckBox;
		private var mltfPaste:MLTextField;

		public function MLDBGEaseEditView(name:String) {
			var MCTextClass:Class=Class(getDefinitionByName('MLDBGEaseEditViewMC'));
			super(new MCTextClass(), name);
			var mc_dropDown:MovieClip=MovieClip(mlmc.mc.getChildByName('dropDown'));
			
			if(!easesInfo) {
				easesInfo=[
					new EaseTypeInfo(MLEase.type_none, []),
					new EaseTypeInfo(MLEase.type_Sin, []),
					new EaseTypeInfo(MLEase.type_Log, []),
					new EaseTypeInfo(MLEase.type_Circ, []),
					new EaseTypeInfo(MLEase.type_CircHard, []),
					new EaseTypeInfo(MLEase.type_Elastic, [['humps', 1, 40, 8], ['dynamic', 0, 5, 1.8], ['power', 0, 5, 1], ['friction', 0, 3, 1]]),
					new EaseTypeInfo(MLEase.type_Bounce, [['humps', 1, 40, 5], ['power', 0, 5, .5], ['friction', 0, 3, 1]]),
					new EaseTypeInfo(MLEase.type_Pow, [['', 0, 10, 2]]),
					new EaseTypeInfo(MLEase.type_Bezier, [['', -2, 2, 0], ['', -2, 2, 0]]),
					new EaseTypeInfo(MLEase.type_Bezier2, [['', -2, 2, 0], ['', -2, 2, 0], ['', -2, 2, 0]])
				];
				easesListElements=[];
				var i:uint;
				var loop:EaseTypeInfo;
				for(i=0;i<easesInfo.length;i++){
					loop=easesInfo[i];
					easesListElements.push(new MCComboBoxItemVO(loop.name, loop));
				}
			}
			
			easesScrolledArray=new ScrollableArray(easesListElements);
			easesListComboBox=new MCComboBox(mc_dropDown, false);
			easesListComboBox.setModel(easesScrolledArray);
			easesListComboBox.addListener(this);
			
			sliders=new EasePropsSliders(mlmc.getMC('sliders'));
			sliders.setEaseInfo(easesInfo[5]);
			sliders.addListener(this);
			
			chart=new EaseChart(mlmc.getMC('chart'));
			
			var mc_checkDouble:MovieClip=MovieClip(mlmc.mc.getChildByName('checkDouble'));
			checkDouble=new MCCheckBox(mc_checkDouble, false);
			checkDouble.addListener(this);
			
			mltfPaste=MLTextField.forInstance(mlmc.getTextField('tfPaste'));
			mltfPaste.tf.htmlText='<U><A HREF="event:paste">'+mltfPaste.tf.text+'</A></U>';
			mltfPaste.addListener(this);
		}
		
		private function pasteEasesDataToClippboard():void {
			var classStr:TabsText=new TabsText();
			classStr.plusTab();			classStr.plusTab();
			var easesArr:Array=MLDBGEasesList.getInstance().getEases();
			var i:uint,iP:uint,ease:MLEase,easeProps:Array;
			for(i=0;i<easesArr.length;i++){
				ease=easesArr[i];
				classStr.addLine('public static var ease_'+ease.name+' : MLEase;');
			}
			classStr.addLine('');
			classStr.addLineAndPlus('public static function setupEases():void {');
			for(i=0;i<easesArr.length;i++){
				ease=easesArr[i];
				easeProps=ease.getEaseProps();
				for(iP=0;iP<easeProps.length;iP++){
					easeProps[iP]=int(easeProps[iP]*100)/100;
				}
				classStr.addLine("ease_"+ease.name+"=MLEase.getInstanceForName('"+ease.name+"').setupEase"+ease.getType()+"("+easeProps.join(', ')+");");
			}
			classStr.minusAndAddLine('}');
			
			System.setClipboard(classStr.toString());
		}
		
		override public function getPrivateVariables():Array {
			return [
				['sliders', sliders]
			].concat(super.getPrivateVariables());
		}
		
		public function setModel(model:MLEase):void {
			dbg.log('model>'+dbg.link(model));
			this.model=model;
			setEaseType(EaseTypeInfo(MLArrayUtil.getWhereObjProp(easesInfo, 'name', model.getType())));
			sliders.setEase(model);
			chart.setModel(model);
			checkDouble.getModel().v=model.getIsRevertFilter();
			
		}
		
		protected function setEaseType(easeType:EaseTypeInfo):void {
			modelEaseType=easeType;
			easesListComboBox.setSelectedItemByData(modelEaseType);
			sliders.setEaseInfo(modelEaseType);
		}
		
		protected function setNewEaseType(type:EaseTypeInfo):void {
			switch(type.name){
				case MLEase.type_none:
					model.setupEaseNone();
				break;
				case MLEase.type_Sin:
					model.setupEaseSin();
				break;
				case MLEase.type_Log:
					model.setupEaseLog();
				break;
				case MLEase.type_Circ:
					model.setupEaseCirc();
				break;
				case MLEase.type_CircHard:
					model.setupEaseCircHard();
				break;
				case MLEase.type_Elastic:
					model.setupEaseElastic(type.params[0][3], type.params[1][3], type.params[2][3], type.params[3][3]);
				break;
				case MLEase.type_Bounce:
					model.setupEaseBounce(type.params[0][3], type.params[1][3], type.params[2][3]);
				break;
				case MLEase.type_Pow:
					model.setupEasePow(type.params[0][3]);
				break;
				case MLEase.type_Bezier:
					model.setupEaseBezier(type.params[0][3], type.params[1][3]);
				break;
				case MLEase.type_Bezier2:
					model.setupEaseBezier2(type.params[0][3], type.params[1][3], type.params[2][3]);
				break;
			}
			setEaseType(type);
			sliders.setEase(model);
			chart.draw();
		}
		
		override public function getIsScrollable():Boolean { return false; }
		
		override protected function drawData():void {
			
		}
		
		override protected function doSize():void {
			
		}
		
		override protected function doIsEnabled():void {
			
		}
		
		override protected function doIsDisabled():void {
			
		}
		
//********************************************************************************************
//		events for MLDBGEaseEditView
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case sliders:
					switch(ev.event) {
						case sliders.event_RequestChange:
							var i:uint;
							var loop:Array;
							var modelNewEaseProps:Array=model.getEaseProps();
							for(i=0;i<modelEaseType.params.length;i++){
								loop=modelEaseType.params[i];
								loop[3]=modelNewEaseProps[i];
							}
							chart.draw();
						break;
					}
				break;
				case easesListComboBox:
					switch(ev.event) {
						case easesListComboBox.event_Selected:
							setNewEaseType(EaseTypeInfo(MCComboBoxItemVO(ev.data).data));
						break;
					}
				break;
				case checkDouble:
					switch(ev.event) {
						case checkDouble.event_RequestChange:
							checkDouble.getModel().switch_();
							model.setIsRevertFilter(checkDouble.getModel().v);
							chart.draw();
						break;
					}
				break;
				case mltfPaste:
					switch(ev.event) {
						case mltfPaste.event_Link:
							pasteEasesDataToClippboard();
						break;
					}
				break;
			}
		}
		
	}
}
